package com.smh;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author shiminghui
 * @date 2025/2/9 19:32
 * @description: TODO
 */
public class _012_数组 {
    @Test
    public void test01() {

    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = 0;
        int j = 0;
        int res[] = new int[m + n];

        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                res[i + j] = nums1[i];
                i++;
            } else {
                res[i + j] = nums2[j];
                j++;
            }
        }
        while (i < m) {
            res[i + j] = nums1[i];
            i++;
        }
        while (j < n) {
            res[i + j] = nums2[j];
            j++;
        }
        System.arraycopy(res, 0, nums1, 0, res.length);
        return;
    }


    public void merge1(int[] nums, int i, int iEnd, int j, int jEnd, int[] res, int k) {

        if (i > iEnd) {
            while (j <= jEnd) {
                res[k++] = nums[j++];
            }
            return;
        }
        if (j > jEnd) {
            while (i <= iEnd) {
                res[k++] = nums[i++];
            }
            return;
        }

        if (nums[i] < nums[j]) {
            res[k] = nums[i];
            merge1(nums, i + 1, iEnd, j, jEnd, res, k + 1);
        } else {
            res[k] = nums[j];
            merge1(nums, i, iEnd, j + 1, jEnd, res, k + 1);
        }


        /*while (i <= iEnd && j <= jEnd){
            if (nums[i] < nums[j]){
                res[k++] = nums[i++];
            }else {
                res[k++] = nums[j++];
            }
        }
        while (i <= iEnd){
            res[k++] = nums[i++];
        }
        while (j <= jEnd){
            res[k++] = nums[j++];
        }*/
    }

}
